<template>
  <view v-if="cart.length!==0">
    <my-address></my-address>
    <!-- 商品列表标题区域 -->
    <view class="good-title-box">
      <uni-icons type="shop" size="26" class="title-icon"></uni-icons>
      <text class="title-text">购物车</text>
    </view>

    <!-- 滑动删除 ,购物车列表-->
    <uni-swipe-action>
      <!-- 商品内容列表 -->
      <view class="goods-list-box" v-for="(item, index) in cart" :key="index">
        <uni-swipe-action-item :right-options="options" @click="clickSwipeItemHandler(item)">
          <my-goods
            :item="item"
            :count_show="true"
            @isCheck="
              (value) => {
                toggleCheck(value);
              }
            "
            @handler-count="inCartHandlerCount"
          ></my-goods>
        </uni-swipe-action-item>
      </view>
    </uni-swipe-action>
    <!-- 结算栏 -->
    <view class="footer-fixed">
      <div class="all-check">
        <van-checkbox icon-size="18" :value="isAllChecked" @change="toggleAllCheck"></van-checkbox>
        全选
      </div>
      <div class="all-total">
        <div class="price-box">
          <span>合计：</span>
          <span class="price">
            ¥
            <i class="totalPrice">{{ getCheckedPrice }}</i>
          </span>
        </div>
        <div v-if="!isEdited" class="goPay" :class="{ disabled: total === 0 }" @click="goPay">结算({{ getCheckedCount }})</div>
        <div v-else class="delete" :class="{ disabled: total === 0 }" @click="handleDel">删除</div>
      </div>
    </view>
  </view>
  
  <!-- 空白购物车的区域 -->
  <view class="empty-cart" v-else>
    <image src="/static/cart_empty@2x.png" mode="" class="empty-img"></image>
    <text class="tip-text">空空如也</text>
    
  </view>
</template>

<script>
import badegMin from '@/mixins/tabbar-badeg.js';
import { mapState, mapGetters } from 'vuex';
export default {
  computed: {
    ...mapState('m_cart', ['cart']),
    ...mapGetters('m_cart', ['total', 'getCartTotal', 'getCheckedCart', 'getCheckedCount', 'getCheckedPrice', 'isAllChecked'])
  },
  mixins: [badegMin],
  data() {
    return {
      options: [
        {
          text: '删除',
          style: {
            backgroundColor: '#c00000'
          }
        }
      ]
    };
  },
  methods: {
    toggleCheck(goodsId) {
      this.$store.commit('m_cart/toggleCheck', goodsId);
    },
    toggleAllCheck() {
      this.$store.commit('m_cart/toggleAllCheck', !this.isAllChecked);
    },
    inCartHandlerCount({ goodsId, goodsNum }) {
      // console.log("cart:",goodsId,goodsNum);
      this.$store.commit('m_cart/changeCount', { goodsId, goodsNum });
    },
    clickSwipeItemHandler(goods){
      this.$store.commit('m_cart/removeGoodsItemById',goods.goods_id)
      
    }
  }
};
</script>

<style lang="scss">
.good-title-box {
  height: 50px;
  display: flex;
  align-items: center;
  border: 2px solid #dedede;
  padding-left: 5px;
  .title-icon {
    margin-right: 10px;
  }
}

.footer-fixed {
  position: fixed;
  left: 0;
  bottom: 0px;
  height: 50px;
  width: 100%;
  border-bottom: 1px solid #ccc;
  background-color: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 10px;
  .all-check {
    display: flex;
    align-items: center;
    .van-checkbox {
      margin-right: 5px;
    }
  }
  .all-total {
    display: flex;
    line-height: 36px;
    margin-right: 15px;
    .price-box {
      display: flex;
      font-size: 14px;
      margin-right: 10px;
      .price {
        display: flex;
      }
      .totalPrice {
        color: #fa2209;
        font-size: 18px;
        font-style: normal;
      }
    }
    .goPay,
    .delete {
      min-width: 100px;
      height: 36px;
      line-height: 36px;
      text-align: center;
      background-color: #fa2f21;
      color: #fff;
      border-radius: 18px;
      &.disabled {
        background-color: #ff9779;
      }
    }
  }
}

.empty-cart{
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 150px;
  .empty-img{
    width: 90px;
    height: 90px;
  }
  .tip-text{
    color: gray;
    margin-top: 15px;
  }
  
}
</style>
